package com.ruoyi.gcgl.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.gcgl.mapper.GcQttkqrdMapper;
import com.ruoyi.gcgl.domain.GcFyhx;
import com.ruoyi.gcgl.domain.GcQttkqrd;
import com.ruoyi.gcgl.service.IGcQttkqrdService;
import com.ruoyi.common.core.text.Convert;

/**
 * 其它退款确认单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-17
 */
@Service
public class GcQttkqrdServiceImpl implements IGcQttkqrdService 
{
    @Autowired
    private GcQttkqrdMapper gcQttkqrdMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    IdentityService identityService;

    /**
     * 查询其它退款确认单
     * 
     * @param id 其它退款确认单主键
     * @return 其它退款确认单
     */
    @Override
    public GcQttkqrd selectGcQttkqrdById(Long id)
    {
        return gcQttkqrdMapper.selectGcQttkqrdById(id);
    }

    /**
     * 查询其它退款确认单列表
     * 
     * @param gcQttkqrd 其它退款确认单
     * @return 其它退款确认单
     */
    @Override
    public List<GcQttkqrd> selectGcQttkqrdList(GcQttkqrd gcQttkqrd)
    {
        return gcQttkqrdMapper.selectGcQttkqrdList(gcQttkqrd);
    }

    /**
     * 新增其它退款确认单
     * 
     * @param gcQttkqrd 其它退款确认单
     * @return 结果
     */
    @Override
    public int insertGcQttkqrd(GcQttkqrd gcQttkqrd)
    {
    	int rows = gcQttkqrdMapper.insertGcQttkqrd(gcQttkqrd);
    	// 发起合同审核流程
        identityService.setAuthenticatedUserId(gcQttkqrd.getOwner());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", gcQttkqrd.getOwner());
        variables.put("level1", gcQttkqrd.getApprovename());
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjfeiyonghuakou", String.valueOf(gcQttkqrd.getId()), variables);
    	
        GcQttkqrd obj = new GcQttkqrd();
    	obj.setId(gcQttkqrd.getId());
    	obj.setProcessInstanceId(pi.getProcessInstanceId());
    	gcQttkqrdMapper.updateGcQttkqrd(obj);
    	
        return rows;
    }

    /**
     * 修改其它退款确认单
     * 
     * @param gcQttkqrd 其它退款确认单
     * @return 结果
     */
    @Override
    public int updateGcQttkqrd(GcQttkqrd gcQttkqrd){
    	// 发起合同审核流程
//        identityService.setAuthenticatedUserId(gcQttkqrd.getOwner());
//        HashMap<String, Object> variables = new HashMap<>();
//        variables.put("applyuserid", gcQttkqrd.getOwner());
//        variables.put("level1", gcQttkqrd.getApprovename());
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjfeiyonghuakou", String.valueOf(gcQttkqrd.getId()), variables);
//        gcQttkqrd.setProcessInstanceId(pi.getProcessInstanceId());
        return gcQttkqrdMapper.updateGcQttkqrd(gcQttkqrd);
    }

    /**
     * 批量删除其它退款确认单
     * 
     * @param ids 需要删除的其它退款确认单主键
     * @return 结果
     */
    @Override
    public int deleteGcQttkqrdByIds(String ids)
    {
        return gcQttkqrdMapper.deleteGcQttkqrdByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除其它退款确认单信息
     * 
     * @param id 其它退款确认单主键
     * @return 结果
     */
    @Override
    public int deleteGcQttkqrdById(Long id)
    {
        return gcQttkqrdMapper.deleteGcQttkqrdById(id);
    }

	@Override
	public int updateGcQttkqrd4Check(GcQttkqrd gcQttkqrd) {
		return gcQttkqrdMapper.updateGcQttkqrd(gcQttkqrd);
	}
}
